package top.oylan.manager.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Action;
import top.oylan.manager.entity.extend.ActionExtend;
import top.oylan.manager.service.ActionService;
import top.oylan.manager.tools.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by A on 2019/2/12.
 */
@Controller
@RequestMapping("/admin/action")
public class AdminActionController {

    @Resource
    ActionService actionService;

    @RequestMapping(value = {"/list","","/"})
    public String index(ModelMap modelMap,
                        @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo){

        List<ActionExtend> list = quickSort(actionService.selectAllActions(),new ArrayList<>(),0);
        modelMap.addAttribute("list",list);
        return "admin/action/list";
    }


    @RequestMapping("/add")
    public String add(ModelMap modelMap,
                      @RequestParam(value = "id",defaultValue = "0")Integer id){

        List<Action> list = null;
        if(id == 0){
            list = actionService.selectActionByParentId(0);
        } else{
            Action action = actionService.selectActionById(id);
            list = new ArrayList<Action>();
            list.add(action);
        }
        modelMap.addAttribute("parents",list);
        return "admin/action/add";
    }


    @RequestMapping("/edit")
    public String edit(ModelMap modelMap,
                       @RequestParam(value = "id",defaultValue = "0")Integer id){

        List<ActionExtend> list = actionService.selectAllActions();
        modelMap.addAttribute("parents",list);

        Action action = actionService.selectActionById(id);
        modelMap.addAttribute("action",action);
        return "admin/action/edit";
    }


    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestBody Action action) {
        Result result = new Result();
        try{
            if(null != action.getId() && action.getId() != 0){
                actionService.update(action);
            }else{
                action.setCreateTime(new Date());
                actionService.add(action);
            }
            result.setCode((byte) 1);
        }catch (Exception e){
            result.setCode((byte) 0);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestParam(value = "id",defaultValue = "0")Integer id){
        Result result = new Result();
        try {
            actionService.delete(id);
            result.setCode((byte) 1);
        }catch (Exception e){
            result.setMessage(e.getMessage());
            result.setCode((byte) 0);
        }

        return result;
    }

    /**
     * 快速排序
     * @param list
     * @return
     */
    public List<ActionExtend> quickSort(List<ActionExtend> list,List<ActionExtend> result,Integer parentId){
        for(ActionExtend parent:list){
            if(parent.getParentId() == parentId){
                result.add(parent);
                quickSort(list,result,parent.getId());
            }
        }
        return result;
    }

}
